package cn.codeforfun.dao;

import cn.codeforfun.entity.Student;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by Administrator on 2016/9/22.
 */
@Repository
public class StudentDao {
    private static Map<Integer, Student> studentMap;

    static {
        studentMap = new HashMap<Integer, Student>() {
            {
                put(1, new Student(1, "张三"));
                put(2, new Student(2, "李四"));
                put(3, new Student(3, "王五"));
                put(4, new Student(4, "赵六"));
            }
        };
    }

    public Collection<Student> findAll() {
        return studentMap.values();
    }

    public void save(Student student) {
        if (student.getId() == null) {
            Set<Integer> integers = studentMap.keySet();
            Integer id = 1;
            for (Integer id1 : integers) {
                if (id1 > id) id = id1;
            }
            id++;
            student.setId(id);
            studentMap.put(id, student);
            return;
        }
        studentMap.put(student.getId(), student);
    }

    public Student get(Integer id) {
        return studentMap.get(id);
    }

    public void delete(Integer id) {
        studentMap.remove(id);
    }
}
